package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoComment;
import com.tanhua.model.mongo.VideoCommentLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class VideoCommentApiImpl implements VideoCommentApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //判断用户是否已点赞小视频
    @Override
    public Boolean hasComment(String videoId, Long userId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("videoId").is(new ObjectId(videoId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, VideoComment.class);
        return exists;
    }

    //保存用户点赞该小视频
    @Override
    public Integer save(VideoComment videoComment) {
       //保存videoComment到MongoDB
        mongoTemplate.save(videoComment);
        //修改视频表中的总数量
        Query query = Query.query(Criteria.where("id").is(videoComment.getVideoId()));
        Update update = new Update();
        if(videoComment.getCommentType() == CommentType.LIKE.getType()){
            //喜欢
            update.inc("likeCount",1);
        }else if(videoComment.getCommentType() == CommentType.LOVE.getType()){
            //关注
            update.inc("loveCount",1);
        }else {
            //评论
            update.inc("commentCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);  //获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //获取最新的点赞数量并返回
        Integer count = modify.statisCount(videoComment.getCommentType());
        return count;
    }

    //删除用户点赞该小视频数据
    @Override
    public Integer delete(VideoComment videoComment) {
        //删除videoComment表数据
        Criteria criteria = Criteria.where("userId").is(videoComment.getUserId())
                .and("videoId").is(videoComment.getVideoId())
                .and("commentType").is(videoComment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,VideoComment.class);
        //2.修改视频表中的总数量
        Query videoQuery = Query.query(Criteria.where("id").is(videoComment.getVideoId()));
        Update update = new Update();
        if(videoComment.getCommentType() == CommentType.LIKE.getType()){
            //喜欢
            update.inc("likeCount",-1);
        }else if(videoComment.getCommentType() == CommentType.LOVE.getType()){
            //关注
            update.inc("loveCount",-1);
        }else {
            //评论
            update.inc("commentCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);  //获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(videoQuery, update, options, Video.class);
        //获取最新的点赞数量并返回
        Integer count = modify.statisCount(videoComment.getCommentType());
        return count;

    }

    /**
     * 判断当前用户是否对该小视频点赞
     * @param userId  用户id
     * @param commentId  小视频id
     * @return
     */
    @Override
    public Boolean hasLike(Long userId, ObjectId commentId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("videoId").is(commentId).and("commentType").is(CommentType.LIKE.getType());
        Query query = Query.query(criteria);
        VideoComment one = mongoTemplate.findOne(query, VideoComment.class);
        if(one == null){
            //未点赞
            return false;
        }
        //已点赞
        else return true;
    }

    //查询小视频评论数
    @Override
    public Long commentCount(String id) {
        Query query = Query.query(Criteria.where("videoId").is(new ObjectId(id)));
        long count = mongoTemplate.count(query,VideoComment.class);
        return count;
    }

    //视频评论点赞
    @Override
    public Integer saveLikeComment(VideoCommentLike videoCommentLike) {
        //保存videoCommentLike
        mongoTemplate.save(videoCommentLike);
        //修改视频评论列表中的总数量
        Query query = Query.query(Criteria.where("id").is(videoCommentLike.getVideoCommentId()));
        Update update = new Update();
        update.inc("likeCount",1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        VideoComment modify = mongoTemplate.findAndModify(query, update, options, VideoComment.class);
        //获取最新的点赞数量返回
        Integer likeCount = modify.getLikeCount();
        return likeCount;
    }

    //视频评论取消点赞
    @Override
    public Integer deleteCommentLike(VideoCommentLike videoCommentLike) {
        //删除videoCommentLike中的数据
        Query likeQuery = Query.query(Criteria.where("videoCommentId").is(videoCommentLike.getVideoCommentId()).and("userId").is(videoCommentLike.getUserId()));
        mongoTemplate.remove(likeQuery,VideoCommentLike.class);
        //修改视频列表中的总数量
        Query query = Query.query(Criteria.where("id").is(videoCommentLike.getVideoCommentId()).and("userId").is(videoCommentLike.getUserId()));
        Update update = new Update();
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        VideoComment modify = mongoTemplate.findAndModify(query, update, options, VideoComment.class);
        Integer likeCount = modify.getLikeCount();
        return likeCount;
    }

    //判断用户是否已点赞评论
    @Override
    public Boolean hasLikeComment(Long userId, String videoCommentId) {
        //查询videoCommentLike表
        Query query = Query.query(Criteria.where("userId").is(userId).and("videoCommentId").is(new ObjectId(videoCommentId)));
        VideoCommentLike videoCommentLike = mongoTemplate.findOne(query, VideoCommentLike.class);
        if(videoCommentLike == null){
            return false;
        }else return true;
    }
}
